﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model.WaiGaoQiao;
using WaiGaoQiaoWeb.Models;
using WaiGaoQiaoWeb.JsonEntities;
using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace WaiGaoQiaoWeb.Controllers
{
    public class AccessInOutController : Controller
    {
        private AccessInOutInfo funinfo = new AccessInOutInfo();
        public ActionResult AccessInOutIndex()
        {
            ViewData["deptName"] = GetdeptName();
            ViewData["Machine"] = GetMachine();
            ViewData["PASSFLAG"] = GetPASSFLAG();
            return View();
        }

        /// <summary>
        /// 获取部门列表（用于绑定下拉框）
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetdeptName()
        {
            IDictionary<int, string> dic = new Dictionary<int, string>();
            DataSet ds = funinfo.GetdeptName();
            dic.Add(-1, "全部");
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                dic.Add(Convert.ToInt32(ds.Tables[0].Rows[i]["Dep_ID"]), ds.Tables[0].Rows[i]["deptName"].ToString().Trim());
            }

            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            return selectList;
        }

        /// <summary>
        /// 获取门名称列表（用于绑定下拉框）
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetMachine()
        {
            IDictionary<int, string> dic = new Dictionary<int, string>();
            DataSet ds = funinfo.GetMachine();
            dic.Add(-1, "全部");
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                dic.Add(Convert.ToInt32(ds.Tables[0].Rows[i]["MACID"]), ds.Tables[0].Rows[i]["设备描述"].ToString().Trim());
            }

            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            return selectList;
        }

        /// <summary>
        /// 获取通过标志列表（用于绑定下拉框）
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetPASSFLAG()
        {
            IDictionary<int, string> dic = new Dictionary<int, string>();
            DataSet ds = funinfo.GetMachine();
            dic.Add(-1, "全部");
            dic.Add(8, "通过");
            dic.Add(0, "未通过");
            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            return selectList;
        }

        public ActionResult GetList(PageVM pvm)
        {
            string query = string.Empty;
            if (string.IsNullOrEmpty(pvm.query))
            {
                query = " and 1=1";
            }
            else
            {
                string[] param = pvm.query.Split(',');
                if (param[0] != "")
                {
                    query += " and 日期时间>='" + param[0].ToString() + "' ";
                }
                if (param[1] != "")
                {
                    query += " and 日期时间<='" + param[1].ToString() + "' ";
                }
                if (param[2] != "-1")
                {
                    query += " and DEP_ID='" + param[2].ToString() + "' ";
                }
                if (param[3] != "")
                {
                    query += " and EMPNO='" + param[3].ToString() + "' ";
                }
                if (param[4] != "")
                {
                    query += " and 姓名 like'%" + param[4].ToString() + "%' ";
                }
                if (param[5] != "-1")
                {
                    query += " and MACID='" + param[5].ToString() + "' ";
                }
                if (param[6] != "-1")
                {
                    query += " and PASSFLAG='" + param[6].ToString() + "' ";
                }
            }
            IList<AccessInOutInfo> funlist = funinfo.GetList(pvm.rp, pvm.page, query);
            JsonFlexiGridData jfgd = new JsonFlexiGridData();
            jfgd.page = pvm.page;
            jfgd.total = funinfo.GetCount(query);
            List<string> v;
            foreach (AccessInOutInfo item in funlist)
            {
                #region
                v = new List<string>();
                v.Add(item.rownumber.ToString());
                v.Add(item.deptName);
                v.Add(item.PName);
                v.Add(item.EmpNo);
                v.Add(item.DoorName.ToString());
                v.Add(item.DateTime.ToString());
                v.Add(item.PASSFLAG.ToString());
                #endregion
                FlexiGridRow fgr = new FlexiGridRow()
                {
                    id = item.rownumber.ToString(),
                    cell = v
                };
                jfgd.rows.Add(fgr);
            }
            return Json(jfgd);
        }

        /// <summary>
        /// 报表导出
        /// </summary>
        /// <returns></returns>
        public ActionResult ReportOut(string StartTime, string EndTime, string DEP_ID, string EMPNO, string PNAME, string machine, string passflag)
        {
            return EContent(StartTime, EndTime, DEP_ID, EMPNO, PNAME, machine, passflag);
        }

        public ExcelContentResult EContent(string startTime, string endTime, string depid, string empno, string pname, string machine, string passflag)
        {
            string query = string.Empty;
            query = " and 1=1 ";
            if (startTime != "")
            {
                query += " and 日期时间>='" + startTime.ToString() + "' ";
            }
            if (endTime != "")
            {
                query += " and 日期时间<='" + endTime.ToString() + "' ";
            }
            if (depid != "-1")
            {
                query += " and DEP_ID='" + depid.ToString() + "' ";
            }
            if (empno != "")
            {
                query += " and EMPNO='" + empno.ToString() + "' ";
            }
            if (pname != "")
            {
                query += " and 姓名 like'%" + pname.ToString() + "%' ";
            }
            if (machine != "-1")
            {
                query += " and MACID='" + machine.ToString() + "' ";
            }
            if (passflag != "-1")
            {
                query += " and PASSFLAG='" + passflag.ToString() + "' ";
            }

            StringBuilder sBuilder = new StringBuilder();
            DataSet list = funinfo.GetListAll(query);
            sBuilder.Append("<table cellspacing='0' cellpadding='5' rules='all' border='1'>");
            sBuilder.Append("<tr style='white-space: nowrap;'>");
            sBuilder.Append("<td colspan='6' style='font-size:25px; text-align:center;'>门禁进出记录表</td>");
            sBuilder.Append("</tr>");
            sBuilder.Append("<tr style='font-size:17px; white-space: nowrap;'>");
            sBuilder.Append("<td>部门名称</td>");
            sBuilder.Append("<td>姓名</td>");
            sBuilder.Append("<td>人员编号</td>");
            sBuilder.Append("<td>门名称</td>");
            sBuilder.Append("<td>日期时间</td>");
            sBuilder.Append("<td>通过标志</td>");
            sBuilder.Append("</tr>");
            foreach (DataRow item in list.Tables[0].Rows)
            {
                sBuilder.Append("<tr style='font-size:14px; white-space: nowrap;'>");
                sBuilder.Append("<td>" + item["部门名称"].ToString() + "</td>");
                sBuilder.Append("<td>" + item["姓名"].ToString() + "</td>");
                sBuilder.Append("<td>" + item["人员编号"].ToString() + "</td>");
                sBuilder.Append("<td>" + item["门名称"].ToString() + "</td>");
                sBuilder.Append("<td>" + item["日期时间"].ToString() + "</td>");
                sBuilder.Append("<td>" + item["通过标志"].ToString() + "</td>");
                sBuilder.Append("</tr>");
            }
            sBuilder.Append("</table>");
            ExcelContentResult econtent = new ExcelContentResult();
            econtent.Content = sBuilder.ToString();
            econtent.filename = "DoorInOutReport";
            econtent.ContentType = "application/ms-excel";
            econtent.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
            return econtent;
        }
    }
}
